home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17481 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. From: ChrisHines@msn.com (Chris Hines)
  2. Subject: Re: C++ beginner quesion on data member access.
  3. Date: 16 Apr 96 03:52:53 -0700
  4. References: <4kgb76$r3s@HOPPER.ACM.ORG> <316E7140.C5D@platinum.com>
  5. Message-ID: <00001a81+0000b22d@msn.com>
  6. Path: news.msn.com!msn.com
  7. Newsgroups: comp.lang.c++
  8. Organization: The Microsoft Network (msn.com)
  9.  
  10. Michael Scott wrote:
  11.  
  12. >Ken Varn wrote:
  13. >> 
  14. >>      I have been struggling with the proper way to declare and access data
  15. >> members in my classes.  I was once told that a class should not allow any
  16. >> data members to be public and that they should only be accessed via member
  17. >> functions.  Why?  I know the reasons for declaring private data, 
  18. but what do
  19. >> I gain if all I am doing is providing a member function to get the private
  20. >> data or set the private data.  i.e. why call a getData() function that
  21. >> basically just returns the private data member as opposed to just declaring
  22. >> the pviate data member as public.
  23. >
  24. >Classes encapsulate the behavior of an object.  Data members 
  25. >represent the "state" of an object.  Data access functions 
  26. >provide access to the internal state of an object while allowing 
  27. >the implementation of that state to vary without necessarily 
  28. >changing the interface to the object.  
  29. >
  30. >Pragmatically speaking though, there are classes and 
  31. >applications where the trade-offs between encapsulation overhead
  32. >and direct public member access favor the public data.
  33. >
  34. >Balancing the trade-offs is what makes programming an art rather 
  35. >than a strict science.
  36.  
  37. In addition to what Michael says there is at least one other 
  38. compelling reason to provide data access functions:  To provide 
  39. read-only access to the data members.  As long as the function does 
  40. not return a pointer or reference to the private data member, the 
  41. application outside of the class cannot modify the state of an object 
  42. of the class.  (Actually that's not entirely true.  One can always 
  43. obtain a pointer to an object and [knowing the offset of the member 
  44. from the beginning of the object] write directly into the object's 
  45. memory space.  Doing so is considered highly abusive, however.)
  46.  
  47. Besides the benefits I can't imagine that there is much loss in terms 
  48. of encapsulation overhead if simple access functions are declared 
  49. inline.  In addition, declaring closely related classes or functions 
  50. as friends of your class allow them to access the members of your 
  51. class objects freely without publicizing your members.
  52.  
  53. -------
  54. ChrisHines@msn.com
  55.